//
//  EventService.m
//  aloqa
//
//  Created by mkoo sun on 12-8-24.
//  Copyright (c) 2012年 exodus. All rights reserved.
//

#import "EventService.h"

@implementation EventService

//-------------------------------------------------------------------------------------------
- (id)init
{
    self = [super init];
    if (self) {
        
        registers = [[NSMutableDictionary alloc]init];
    }
    return self;
}

- (void) dealloc
{
    [registers release];
    registers = nil;    
    [super dealloc];
}

//-------------------------------------------------------------------------------------------
-(void) registerEvent:(NSString*)event_id netEvent:(bool)newEvent
{
//    if ( [event_id isEqualToString:TransactionConstant_event_location] && newEvent)
//    {
//        if (trigger == Nil)
//            trigger = [[LocationTrigger alloc]init];
//        
//        [trigger start];
//        
//        triggerStub = [self registerEventListener:TransactionConstant_event_req_location  listener:trigger];
//    }
}

//-------------------------------------------------------------------------------------------
-(void) unregisterEvent:(NSString*)event_id removeEvent:(bool)removeEvent
{
//    if ([event_id isEqualToString:TransactionConstant_event_location] && removeEvent)
//    {
//        [trigger stop];
//        
//        [self unregisterEventListener:TransactionConstant_event_req_location stub:triggerStub];
//    }
}

//-------------------------------------------------------------------------------------------
-(int) registerEventListener:(NSString*)event_id listener:(id)listener
{
    int stub = -1;
    
    @synchronized(registers)
    {
        if (listener == Nil)
            return 0;
        
        bool newEvent = false;
        
        NSMutableArray* array = [registers objectForKey:event_id];
        
        if(array == Nil)
        {
            array = [[[NSMutableArray alloc]init ] autorelease];
            [registers setObject:array forKey:event_id];
            
            newEvent = true;
        }
//        else if([array containsObject:listener])
//        {
//            stub = [array indexOfObject:listener];
//            return stub;
//        }
        
        [array addObject:listener];
        [self registerEvent:event_id netEvent:newEvent];
        
        stub = [array indexOfObject:listener];
    }
    
    return stub;
}

//-------------------------------------------------------------------------------------------
-(void) unregisterEventListener:(NSString*)event_id stub:(int)stub
{
    @synchronized(registers)
    {
        NSMutableArray* array = [registers objectForKey:event_id];
        
        if(array)
        {
            bool removeEvent = FALSE;
            
            //[array replaceObjectAtIndex:stub withObject:Nil ];
            [array replaceObjectAtIndex:stub withObject:@"" ];
            
            int count = 0;
            for(int i=0; i<[array count]; i++)
            {
                if([array objectAtIndex:i])
                    count++;
            }
            if(count == 0)
            {
                removeEvent = TRUE;
            } 
            
            [self unregisterEvent:event_id removeEvent:removeEvent];
        }
    }
}

//-------------------------------------------------------------------------------------------
-(int) signEvent:(NSString*)event_id param1:(NSString*)param1 param2:(NSString*) param2
{
    NSArray* userInfo = [[[NSArray alloc] initWithObjects:event_id, param1, param2, nil] autorelease];
    
    NSMutableArray* listeners = [registers objectForKey:event_id];
    
    int count = 0;
    if (listeners == Nil || (count = [listeners count]) == 0)
        return count;
    
    int listenCount = 0;
    for(int i=0; i<count; i++)
    {
        id lis = [listeners objectAtIndex:i];
        
        if(lis != Nil)
        {
            //[lis onEvent:event_id param1:param1 param2:param2];
            
            listenCount++;
        }
    }
    
    
//    [self performSelectorOnMainThread:@selector(timerTask:) withObject:userInfo waitUntilDone:NO];
    
    [NSTimer scheduledTimerWithTimeInterval:0 
                            target:self 
                            selector:@selector(timerTask:) 
                            userInfo:userInfo repeats:NO];

    return listenCount;
}

//-------------------------------------------------------------------------------------------
-(void) timerTask: (NSTimer*) timer
//-(void) timerTask: (NSArray*) userInfo
{
    NSArray* userInfo = [timer userInfo];
    NSString* event_id = nil;
    NSString* param1 = nil;
    NSString* param2 = nil;
    
    if([userInfo count]>0)
        event_id = [userInfo objectAtIndex:0];
    if([userInfo count]>1)
        param1 = [userInfo objectAtIndex:1];
    if([userInfo count]>2)
    param2 = [userInfo objectAtIndex:2];
    
    @synchronized(registers)
    {
        NSMutableArray* listeners = [registers objectForKey:event_id];
        
        int count = 0;
        if (listeners == Nil || (count = [listeners count]) == 0)
            return;
        int j = [listeners count];
        for(int i = 0; i < j; i++)
        {
            NSObject* lis = [listeners objectAtIndex:i];
            //id lis = [listeners objectAtIndex:i];
            
            if(lis != Nil && ![lis isKindOfClass:[NSString class]])
            //if(lis != Nil)
            {
                [lis onEvent:event_id param1:param1 param2:param2];
            }
        }
    }
}

@end
